package com.hzspace.elasticsearch.component;

import com.hzspace.elasticsearch.entity.Article;
import com.hzspace.elasticsearch.repository.ArticleRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
//import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;

/**
 * TODO
 *
 * @Version 1.0
 * @Author hz
 * @Date 2020/11/26 17:19
 */
@Component
public class ArticleComponent {

//    @Resource
//    private ElasticsearchRestTemplate elasticsearchTemplate;

    @Resource
    private ArticleRepository articleRepository;

    private static final Pageable pageable = PageRequest.of(0,10);

    
//    public void createIndex() {
//        elasticsearchTemplate.createIndex(Article.class);
//    }

    
//    public void deleteIndex(String index) {
//        elasticsearchTemplate.deleteIndex(index);
//    }

    
    public void save(Article Article) {
        articleRepository.save(Article);
    }

    
    public void saveAll(List<Article> list) {
        articleRepository.saveAll(list);
    }

    
    public Iterator<Article> findAll() {
        return articleRepository.findAll().iterator();
    }

    
    public Page<Article> findByContentLike(String content) {
        return articleRepository.findByContentLike(content,pageable);
    }
    public Page<Article> findByContent(String content) {
        return articleRepository.findByContent(content,pageable);
    }
    
    public Page<Article> query(String key) {
        return articleRepository.findByContentLike(key,pageable);
    }

}
